package sisicotren.ui.windows;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import sisicotren.util.Log;

import java.awt.GridBagConstraints;

import estadistica.ChannelOptimizer;
import estadistica.QueueFunction;

public class OptimizationResult {

	private JDialog jDialog = null;  //  @jve:decl-index=0:visual-constraint="240,53"
	private JPanel jContentPane = null;
	private JPanel jPanel1 = null;
	private JButton jButton = null;
	private JPanel jPanel3 = null;
	private MainWindow owner;
	private JTable resultTable;
	
	public OptimizationResult(MainWindow owner) {
		this.owner = owner;
		this.getJDialog();
	}
	
	public void show() {
		this.jDialog.setVisible(true);
	}
	
	/**
	 * This method initializes jDialog	
	 * 	
	 * @return javax.swing.JDialog	
	 */
	private JDialog getJDialog() {
		if (jDialog == null) {
			jDialog = new JDialog(this.owner.getJFrame());
			jDialog.setTitle("Resultado de la Optimizacion");
			jDialog.setContentPane(getJContentPane());
			jDialog.setLocationByPlatform(true);
			jDialog.setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
			jDialog.setSize(700, 500);
		}
		return jDialog;
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
			jContentPane.add(getJPanel3(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(getJButton(), new GridBagConstraints());
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Aceptar");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// DESHARCODEAR
					owner.setTotalChannels(4);
					jDialog.dispose();
				}
			});
		}
		return jButton;
	}

	
	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jPanel3 = new JPanel(new BorderLayout());
			JScrollPane scroll = new JScrollPane(this.getResultTable());
			jPanel3.add(scroll, BorderLayout.CENTER);
		}
		return jPanel3;
	}

	private QueueFunction optimize() throws Exception {
		int maxChannels = (int) (owner.getAvailableChannelsWidth() / (owner.getChannelWidth() + owner.getInterChannelSpace()));
		int optChannels = 
			ChannelOptimizer.getTotalChannels(
				owner.getPoblationSize(), 
				owner.getMaxToleranceInQueue(),
				owner.getLambda(), 
				owner.getMu(), 
				owner.getMaxChannelsForOptimization());
		
		Log.print(
				"OptimizationResult, Calculando optimo con Lamba [" + 
				owner.getLambda() + "] Mu [" + owner.getMu() + "] Ch [" + optChannels + "]");
		return new QueueFunction(owner.getLambda(), owner.getMu(), optChannels, owner.getPoblationSize());
	}
	
	private JTable getResultTable() {
		if(resultTable == null) {
			QueueFunction qf = null;
			try {
				qf = optimize();
			}
			catch(Exception e) {
				e.printStackTrace();
				return null;
			}
			
			String [] cols = { "Item", "Resultado" };
			Object [][] data = { 
				{"Arribos por minuto (Lambda)", "" + qf.getLambda()},
				{"Clientes que atiende un molinete por minuto (Mú)", "" + owner.getMu()},
				{"Cantidad de Molinetes", "" + qf.getM()},
				{"Tamaño promedio de la Cola", "" + qf.getLc()},
				{"Tiempo promedio de una persona en la cola (mins)","" + qf.getWc()},
				{"Cantidad promedio de personas en la estacion", "" + qf.getL()},
				{"Tiempo promedio de una persona en la estacion (mins)", "" + qf.getW()}
			};

			resultTable = new JTable(new ReadOnlyAbstractTableModel(cols, data));
			resultTable.setFillsViewportHeight(true);
		}
		
		return resultTable;
	}
	
}
